<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Frederic Descamps <lefred@tiscali.be>
 * Copyright (C) 2011-2012 Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

function get_etats($where = '') {
	$requete = "SELECT * FROM etats ".$where." order by value DESC"; 
	$resultat = biblio_db_query($requete);
	if(!$resultat) {
		print "PROBLEME : ".biblio_db_error(); 
		return false;
	}
	$rc = array();
	while ($row = biblio_db_fetch_array($resultat)) {
		$id  = $row['id'];           
		$val = $row['etat'];
		$rc[$id] = $val;   
	}
	return $rc;
}

function get_borrower($book_id = 0) {
	$rc = false;
	if ($book_id == 0)
		return false;

	$requete = "SELECT * FROM prets2 WHERE livre_ID = $book_id AND date_in is null";
	$resultat = biblio_db_query($requete);
	if (!$resultat) 
		return false;

	while ($row = biblio_db_fetch_array($resultat))
    	$rc = $row;
	return $rc;
}

function get_book($id = 0) {
	$rc = false;
	if ($id == 0)
		return false;

	$requete = "SELECT * FROM livres where id = $id";
	$resultat = biblio_db_query($requete);
	if (!$resultat) 
		return false;

    while ($row = biblio_db_fetch_array($resultat))
    	$rc = $row;
	return $rc;
}

function format_title($nom) {
	$title = strtolower(trim($nom));  
	$first = strtoupper( $title[0] );  
	$title[0] =  $first;
	return $title;  
}

function format_author($author = '') {
	$n = explode(",", $author);
	$c = count($n);
	if ($c < 2)
		return strtoupper($author);
	$rc = strtoupper(trim($n[0])).", ";
	for ($i = 1; $i < $c; $i++)
		if ($i == 1)
			$rc .= format_title($n[$i]);
		else
			$rc .= ", ".format_title($n[$i]);
	return $rc;
}

function get_genres($where = '') {
	if ($where == '')
	  $requete = "SELECT * FROM genres order by genre"; 
	else
		$requete = "SELECT * FROM genres ".$where." order by genre"; 
		
  if(!($resultat = biblio_db_query($requete)))
    return false;
  $rc = array();
	while ($row = biblio_db_fetch_array($resultat)) {      
    $id = $row['id'];           
    $rc[$id] = $row['genre'];
  }                                
  return $rc;
}

function add_genre($genre = '') {
	if ($genre == '')
		return false;
	$requete = "insert into genres (genre) values ('$genre')";	
	if(!biblio_db_query($requete) )
		return false;
	return biblio_db_insert_id();
}

function get_location_string($local ='', $biblio ='', $rangee = '') {
	if (!isset($local) || ($local == '') )
		return '';
	$rc = $local;
	if (!isset($biblio) || ($biblio == '') )
		return $rc;
	$rc .= "-->".$biblio;
	if (!isset($rangee) || ($rangee == '') )
		return $rc;
	return $rc."-->".$rangee;
}

function get_locations($where = '') {
	if ($where == '')
	  $requete = "SELECT * FROM locations order by local"; 
	else
		$requete = "SELECT * FROM locations ".$where." order by local"; 
	if(!($resultat = biblio_db_query($requete)))
	  return false;
  $rc = array();
	while ($row = biblio_db_fetch_array($resultat)) 
	{      
    $id = $row['id'];           
		$local_l =  $row['local'];
		$biblio_l =  $row['biblio'];
		$rangee_l =  $row['rangee'];
		$value = get_location_string($local_l, $biblio_l, $rangee_l);
		$rc[$id] = $value;
	}
  return $rc;
}

function add_location($local ='', $biblio ='', $rangee = '') {
	if ($local == '')
		return false;
	$requete = "insert into locations (local,biblio,rangee) values ('$local','$biblio','$rangee')";
	if(!biblio_db_query($requete))
		return false;
	return biblio_db_insert_id();
}

function get_title()
{
   $requete = "select title from config";
   $resultat = biblio_db_query($requete);
   $object=biblio_next_object($resultat);
   return ($object->title);
}

function get_logpath()
{
   $requete = "select logpath from config";
   $resultat = biblio_db_query($requete);
   $object=biblio_next_object($resultat);
   return ($object->logpath);
}

function get_rootpwd()
{
   $requete = "select rootpwd from config";
   $resultat = biblio_db_query($requete);
   $object=biblio_next_object($resultat);
   return ($object->rootpwd);
}

function get_coverspath()
{
   $requete = "select coverspath from config";
   $resultat = biblio_db_query($requete);
   $object=biblio_next_object($resultat);
   return ($object->coverspath);
}

function get_picspath()
{
   $requete = "select picspath from config";
   $resultat = biblio_db_query($requete);
   $object=biblio_next_object($resultat);
   return ($object->picspath);
}

function get_language()
{
   $requete = "select language from config";
   $resultat = biblio_db_query($requete);
   $object=biblio_next_object($resultat);
   return ($object->language);
}

function enable_soap()
{
   $requete = "select enable_soap from config";
   $resultat = biblio_db_query($requete);
   $object=biblio_next_object($resultat);
  if (strtoupper($object->client_soap) == "TRUE")
      return true;
   else
      return false; 
}

function get_soap_client()
{
   $requete = "select client_soap from config";
   $resultat = biblio_db_query($requete);
   $object=biblio_next_object($resultat);
   return ($object->client_soap);
 }

function get_max_record()
{
   $requete = "select max_record from config";
   $resultat = biblio_db_query($requete);
   $object=biblio_next_object($resultat);
   return ($object->max_record);
}

function get_book_amazon_isbn($isbn)
{
#   $proxy_tab = array ( 'proxy_host' => "10.0.10.2",
#   			'proxy_port' => 8080
#		);

	return null;
   $soap_client = get_soap_client();
   $client = new SoapClient ($soap_client);

   $params   = array(
    'asin'	   => "$isbn",
    'mode'         => 'books',
    'sort'         => '+title',
    'type'         => 'lite',
    'tag'          => 'trachtenberg-20',
    'devtag'       => 'XXXXXXXXXXXXXX',
    'locale'	   => 'fr'
   );
   $return=$client->ASINSearchRequest($params);
   return($return);
}

function return_isbn($rechcode)
{
  if (strlen($rechcode) == 13)
  {
    #recreate the isbn
    $rechcode=substr($rechcode,3);
    $rechcode=substr($rechcode,0,-1);
    $check=0;
    for ($i=0; $i<strlen($rechcode); $i++)
    {
       $check=$check+($i+1)*$rechcode[$i];
    }
    $rechcode=$rechcode . $check % 11;
  }
  return($rechcode);
}
?>
